
var upload = require('../../utils/upload.js');
const util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:"申请退货",
    reasonLists: ["质量问题","商品与页面描述不符","缺少件"],
    // ,"材质、面料与商品描述不符", "宝贝有损坏", "不喜欢，效果不好", "大小尺寸与商品描述不符", "收到商品少见、破损或污渍", "其他"
    hideShopPopup:true,
    reasonContent:"",
    content:"",
    pics: [],
    isSuccess:false,
    hasLog:false,
    isComplete:false,
    logInfo:{},
    hideLogPopup:true,
    logLists:[],
    hideTypePopup:true,
    typeContent:"退款",
    typeLists:["换货","退款","退货退款"],
    price:0
  },
  closeTypePopupTap(){
    this.setData({
      hideTypePopup:true
    })
  },
  // textarea blur
  blure(e) {
    this.setData({
      content: e.detail.value
    })
  },
  // 关闭
  closeLogPopupTap(){
    this.setData({
      hideLogPopup:true
    })
  },
  showPopu(){
    this.setData({
      hideLogPopup: false
    })
  },
  // 去逛商城
  goIndex(){
    // 返回售后列表
    if((this.data.status =="已拒绝"||this.data.status =="已发货"||this.data.orgin)){
      wx.navigateBack({
        delta: 1,
      })
    }else{ // 去首页
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },
  // select 更新
  change(e){
    console.log("更新",e)
  },
  // 确定提交
  confrim(){
    console.log("确认提交",this.data)
    let that = this, header = app.getPostRequestHeader();;
    wx.request({
      url: app.globalData.urls + '/order/add-process',
      method: "POST",
      header: {
        "access-token": header['access-token'],
        "fecshop-currency": header['access-token'],
        "fecshop-lang": "zh"
      },
      data: {
        item_id: this.data.item_id,
        application_type: this.data.typeContent, //==1 ? '换货' :'退款',
        application_reason: this.data.reasonContent,
        application_remark: this.data.content,
        pic: this.data.pics,
        return_money:this.data.price
      },
      success: function (res) {
        if(res.data.code==200){
          wx.showToast({
            title: '提交成功，即将返回',
          })
          setTimeout(()=>{
            wx.navigateTo({
              url: "/pages/return-after/return-after" 
            })
          },1000)
        }else{
          wx.showToast({
            title: res.data.message,
          })
        }
      }
    });
  },
  logTap(e){
    this.setData({
       "logInfo.tracking_company":e.currentTarget.dataset.item.tracking_company,
       "logInfo.company_code": e.currentTarget.dataset.item.company_code
    })
  },
  // 删除图片
  delimg(e) {
    var idx = e.currentTarget.dataset.index;
    this.data.pics.splice(idx, 1);
    this.setData({
      pics: this.data.pics
    })
  },
  // 上传图片
  upShopLogo: function () {
    upload.upShopLogo(res=>{
      let pics = this.data.pics;
      pics.push(res.data.url)
      this.setData({
        pics: pics
      })
    })

  },
  /**
   * 生命周期函数--监听页面加载 order type=1换货2退货  3-发货
   * id -- 售后服务id
   */
  onLoad: function (options) {
    if (options.type == 1){ // 换货
      this.setData({
        title: "申请换货",
        typeContent:'换货'
      })
    }
    this.setData({
      price:options.price||0,
      money:options.price||0
    })
    if(options.type == 3){ // 发货
      this.setData({
        orgin:true,
        manage:options.title,
        title: options.title || '发货'
      })
    }
    // 4 - 退款
    if(options.type == 4){ // 退款
      this.setData({
        orgin:true,
        isSuccess:true,
        title: options.title
      })
    }
    // 5 - 换货 --拒绝换货
    if(options.type == 5){ // 退换货  -- 换货
      this.setData({
        orgin:true,
        isSuccess:false,
        title: options.title
      })
      if(!options.success){
        this.setData({
          type:3,
        })
        options.type=3;
      }
      this.getBdminInfo(options.id,options.status_text);
    }
    // 6 - 换货 --退货退款申请
    if(options.type == 6){ // 退货退款申请
      this.setData({
        orgin:true,
        isSuccess:false,
        title: options.title
      })
      options.type=options.status||options.type
      if(options.status!=5){
        this.getBdminInfo(options.id,options.status_text);
      }
      this.setData({
        typeContent:(this.data.type==1 && '换货' || '退款')
      })
      
    }
    

    // 订单管理 - 发货信息、退款信息
    if(options.info){
      let m=JSON.parse(options.info);
      this.setData({
        logInfo:{
          contract_name:m.customer_firstname||m.firstname,
          contract_phone:m.customer_telephone||m.phone,
          contract_address:m.customer_address_state+m.customer_address_city+m.customer_address_street1,
          tracking_company:'',
          tracking_number:'',
          ...m
        }
      })

    }


    this.setData(options)
    // 
    if(options.type!=6 && options.status!=5){
      if (options.id){
        this.getLogInfo()
      }
      this.getCompany()
    }
  },
  // 获取商家的信息 customer
  getBdminInfo(id,status_text){
    let that = this
    wx.request({
      url: app.globalData.urls + (status_text=='after_sale_return_admin_received' ? '/order-manage/customer-return-info': '/order-manage/return-info'),
      method: "POST",
      header: app.getPostRequestHeader(),
      data: {
        increment_id:id,
        bdmin_user_id:util.getBdmin_user_id()
      },
      success: function (res) {
        let logData = res.data.data;
        console.log("--退换货商家信息-logData",logData)
        if (res.data.code == 200) {
          that.setData({
            logInfo: logData
          })
        }
      }
    })
  },
  // 获取物流公司
  getCompany(){
    let that = this
    wx.request({
      url: app.globalData.urls + '/order/tracking-company',
      method: "POST",
      header: app.getPostRequestHeader(),
      data: {},
      success: function (res) {
        let logData = res.data.data;
        if (res.data.code == 200) {
          let logInfo = that.data.logInfo;

          if (logInfo.tracking_company){
            let getResult=logData.filter(val => val.tracking_company == logInfo.tracking_company)
            logInfo.company_code = getResult[0].company_code
          }
          that.setData({
            logLists: logData
          })
        }
      }
    })
  },
  // 获取物流信息
  getLogInfo(){
    let that = this
    wx.request({
      url: app.globalData.urls + '/order/process-detail',
      method: "POST",
      header: app.getPostRequestHeader(),
      data: {
        id:this.data.id
      },
      success: function (res) {
        let data=res.data.data
        if (res.data.code == 200 && data.length!=0) {
            that.setData({
              logInfo: data
            })
        }
      }
    })
  },
  previewImg(e){
    var index = e.currentTarget.dataset.index;
    var imgArr = pics;
    wx.previewImage({
      current: imgArr[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
    })
  },
  // 点击提交物流信息
  clickConfrim(){
    let that = this
    if(this.data.client==1){
      wx.request({
        url: app.globalData.urls + '/order-manage/after-deliver-customer',
        method: "POST",
        header: app.getPostRequestHeader(),
        data: {
          id:this.data.id,
          tracking_company:this.data.logInfo.tracking_company,
          tracking_number: this.data.logInfo.tracking_number,
          company_code: this.data.logInfo.company_code
        },
        success: function (res) {
          if (res.data.code == 200) {
            that.setData({
              isSuccess: true,
              hasLog:true,
              status:null,
              isComplete:false
            })
          }else{
            wx.showToast({
              title: res.data.message,
              icon:"none"
            })
          }
        }
      })
    }else{
      console.log(this.data)
      let that = this
      if(this.data.title=="去换货"){// 换货
        wx.request({
          url: app.globalData.urls + '/order-manage/after-deliver',
          method: "POST",
          header: app.getPostRequestHeader(),
          data: {
            id:this.data.bid,
            tracking_company:this.data.logInfo.tracking_company,
            tracking_number: this.data.logInfo.tracking_number,
            company_code: this.data.logInfo.company_code
          },
          success: function (res) {
            if (res.data.code == 200) {
              that.setData({
                isSuccess: true,
                status:"已发货"
              })
            }else{
              wx.showToast({
                title: res.data.message,
              })
            }
          }
        })
      }else{
        wx.request({
          url: app.globalData.urls + '/order/express-data',
          method: "POST",
          header: app.getPostRequestHeader(),
          data: {
            tracking_number: this.data.id,
            company_code: this.data.logInfo.company_code
          },
          success: function (res) {
            if (res.data.code == 200) {
              that.setData({
                isSuccess: true,
                hasLog:true,
                status:null,
                isComplete:false
              })
            }
          }
        })
  
      }
    }
  },
  // 确认发货   发货
  confrimSendGoods(){
    let that = this
    wx.showLoading();
    if(this.data.client==1){
      wx.request({
        url: app.globalData.urls + '/order-manage/after-deliver-customer',
        method: "POST",
        header: app.getPostRequestHeader(),
        data: {
          id:this.data.id,
          tracking_company:this.data.logInfo.tracking_company,
          tracking_number: this.data.logInfo.tracking_number,
          company_code: this.data.logInfo.company_code
        },
        success: function (res) {
          wx.hideLoading();
          if (res.data.code == 200) {
            that.setData({
              isSuccess: true,
              hasLog:true,
              status:null,
              isComplete:false
            })
          }else{
            wx.showToast({
              title: res.data.message,
              icon:"none"
            })
          }
        }
      })
    }else{
      wx.request({
        url: app.globalData.urls + '/order-manage/deliver',
        method: "POST",
        header: app.getPostRequestHeader(),
        data: {
          order_id:Number(this.data.logInfo.order_id),
          tracking_company:this.data.logInfo.tracking_company,
          tracking_number: this.data.logInfo.tracking_number,
          company_code: this.data.logInfo.company_code
        },
        success: function (res) {
          wx.hideLoading()
          if (res.data.code == 200) {
            that.setData({
              isSuccess: true,
              status:"已发货"
            })
          }else{
            wx.showToast({
              title: res.data.message,
              icon:'none'
            })
          }
        }
      })
    }
    
  },
  // // 去发货
  goSendGoods(){
    this.setData({
      isSuccess:false,
      type:3,
    })
  },
  // 同意退款
  returnMoney(){
    // 发送请求
    // 请求成功以后
    this.setData({
      isSuccess:true,
      status:"已退款",
      type:3,
    })
  },
  // 拒绝换货
  returnChange(){
    this.setData({
      isSuccess:false,
    })
  },
  // 退货退款信息填写完毕---点击提交
  consendConfrim(){
    // 发送请求
    var that=this;
    console.log('----',this.data.logInfo)
    // 发送请求
    wx.request({
      url: app.globalData.urls + '/order-manage/after-accept',
      header: app.getRequestHeader(),
      method: 'POST',
      data: {
        id:this.data.orderId,
        contract_address:this.data.logInfo.contract_address,
        contract_name:this.data.logInfo.contract_name,
        contract_phone:this.data.logInfo.contract_phone,
        address:this.data.logInfo.contract_address
      },
      success: function (res) {
        if(res.data.code==200){
           // 请求成功以后
          that.setData({
            isSuccess:true,
            status:"退货邮寄信息已提交，请等待用户发货",
            type:1,
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon:'none'
          })
        }
      }
    })
   
  },
  // 同意退货退款
  consend(){
    this.setData({
      isSuccess:false,
    })
  },
  // 确定拒绝换货
  reject(){
    var that=this;
    console.log("-----",this.data.content)
    // 发送请求
    wx.request({
      url: app.globalData.urls + '/order-manage/after-refuse',
      header: app.getRequestHeader(),
      method: 'POST',
      data: {
        id:this.data.id,
        refuse_reason:this.data.content
      },
      success: function (res) {
        if(res.data.code==200){
          // 请求成功以后
          that.setData({
            isSuccess:true,
            status:"已拒绝",
            type:1,
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon:'none'
          })
        }
      }
    })
    
  },
  changeInputContent(e){
    let getType = e.currentTarget.dataset.type,val=e.detail.value;
    if(getType=="price"){
      console.log(val,this.data.money)
      if(Number(val)>Number(this.data.money)){
        wx.showToast({
          title: '退款金额不能大于产品金额哦',
          icon:'none'
        })
        this.setData({
         price: this.data.money
        })
      }else{
        this.setData({
          price: val
         })
      }
      
    }else{
      let logInfo = this.data.logInfo
      logInfo[getType] = val;
      this.setData({
        logInfo: logInfo
      })
    }
  },
  // inputBlur
  bindblur(e){
    let getType = e.currentTarget.dataset.type;
    if (getType=="com"){
      this.setData({
        "logInfo.tracking_company": e.detail.value
      })
    }else{
      this.setData({
        "logInfo.tracking_number": e.detail.value
      })
    }
  },
  typeModal(){
    this.setData({
      hideTypePopup:false
    })
  },
  // 申请原因
  reason(){
    this.setData({
      hideShopPopup:false
    })
  },
  // 隐藏弹窗
  closePopupTap(){
    this.setData({
      hideShopPopup: true
    })
  },
  // 更改原因
  changeReason(e){
    let item = e.currentTarget.dataset.item
    this.setData({
      reasonContent: item
    })
  },
  changeRType(e){
    let item = e.currentTarget.dataset.item
    this.setData({
      typeContent: item
    })
  },
})